import { defineStore } from "pinia";
import { Names } from "../../store-namespace";
import { _get, _post, _put, _delete, http } from "@/utils/axios";
import { EquipMWarn, EquipMWarnHandle } from "@/api/backend/index";
import { splitEventList } from "@/utils/common";
import { useBackendMonitor } from "@/store/backend/EquipM/monitor"; //实时设备监控仓库

export const useWarnLog = defineStore(Names.warnLog, {
  state: () => {
    return {
      deviceItem: {
        cname: " ",
        createBy: null,
        createTime: null,
        ecode: "",
        ename: "",
        eqid: 0,
        id: 0,
        params: {},
        pname: "",
        regions: null,
        remark: null,
        reportName: "",
        reportTime: "",
        searchValue: null,
        state: 0,
        updateBy: null,
        updateTime: null,
        userPhone: "",
        username: "",
      }, //告警日志模块获取的单个设备详情数据
      list: [],
      //告警事件
      warn: <any>[],
    };
  },
  getters: {},
  actions: {
    //新增事件处理
    addEventHandle(data: anyObj, type: string) {
      return new Promise((resolve, reject) => {
        _post(EquipMWarnHandle, data, "json", "")
          .then((res: any) => {
            console.log(res);
            resolve("");
          })
          .catch((err) => {
            console.log(err);
            reject(err);
          });
      });
    },
    //修改告警状态
    changeEventHandle(data: anyObj) {
      return new Promise((resolve, reject) => {
        _put(EquipMWarn, data, "json", "")
          .then((res: any) => {
            console.log(res);
            resolve("");
          })
          .catch((err) => {
            console.log(err);
            reject(err);
          });
      });
    },
    //获取告警列表
    getList(filter: any) {
      // const monitor = useBackendMonitor();
      return new Promise((resolve, reject) => {
        _get("/system/warning/listbyregions", filter, "json", "")
          .then((res: any) => {
            console.log("获取告警列表", res);
            this.list = res.rows;
            res.rows.map((item: any) => {
              this.warn.push(splitEventList(item.reportTime));
            });
            resolve(res);
          })
          .catch((err) => {
            console.log(err);
            reject(err);
          });
      });
    },
  },
});
